/*
 * GToe: Gtk+ TicTacToe
 * client.c: connecting player to host
 *
 * Copyright (C) 2010 Dani El-Soufi <danisoufi@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "../include/client.h"
#include "../include/socket.h"
#include "../include/server.h"
#include "../include/player.h"

void
start_client(void)
{
    int transf_bytes, client_socket;
    char buffer[MAXDATASIZE];
    char host_ip_addr[INET6_ADDRSTRLEN + 1];

    printw("\nConnect to host\n");
    printw("Player name: ");

    get_pname(p2, MAX_PNAME_LEN);

    printw("Host to connect to (provide IP or hostname): ");
    /* IMPLEMENT: READ IP */

    printw("Trying to connect..\n");

    client_socket = connect_to_socket(host_ip_addr, SERVER_PORT_NUMBER);

    printw(" connected!\n");
    printw("Listing players:\n");

    transf_bytes = read_from_socket(client_socket, p1.name, sizeof(p1.name));

    printw("    - %s", p1.name);
    printw("(1st Player)\n");

    printw("    - %s", p2.name);
    printw("(you)");

    transf_bytes = write_to_socket (client_socket, p2.name, sizeof(p2.name));

    printw("\nFound 2 players, starting game..\n");
    print_header();

    printw("Random player start, calculating..\n");

    if (!recv_eof)
    {
        transf_bytes = read_from_socket (client_socket, buffer, MAX_PNAME_LEN);

        if (recv_eof)
        {
            return;
        }

        printw("%s will start first\n", buffer);
        printw("Starting the game..");

        init_client();
    }
}

void
init_client()
{
    
}
